﻿#include "dxstdafx.h"
#include "AnalyseTree.h"

bool AnalyseTreeNodeConditionTrue::DoExecute(const AnyData& input, std::wstring& wstrDescription) const
{
	if (nullptr == m_pChildNode.get())
	{
		CommonFunc::ErrorMessageBox(CString("AnalyseTree's operator is too few!"));
		return false;
	}

	return m_pChildNode->DoExecute(input, wstrDescription);
}

bool AnalyseTreeNodeConditionTrue::InsertNode(std::unique_ptr<AnalyseTreeNodeCondition> pNode)
{
	CHECK_RE_FALSE(pNode.get());
	if (m_pChildNode)
	{
		CommonFunc::ErrorMessageBox(CString("repeated node!"));
		return false;
	}
	m_pChildNode = std::move(pNode);
	return true;
}

bool AnalyseTreeNodeConditionNOT::DoExecute(const AnyData& input, std::wstring& wstrDescription) const
{
	if (nullptr == m_pChildNode.get())
	{
		CommonFunc::ErrorMessageBox(CString("NOT's operator is too few!"));
		return false;
	}
	// 从翻译中找到Not的翻译项 再format
	return !m_pChildNode->DoExecute(input, wstrDescription);
}

bool AnalyseTreeNodeConditionNOT::InsertNode(std::unique_ptr<AnalyseTreeNodeCondition> pNode)
{
	CHECK_RE_FALSE(pNode.get());
	if (m_pChildNode)
	{
		CommonFunc::ErrorMessageBox(CString("repeated node!"));
		return false;
	}
	m_pChildNode = std::move(pNode);
	return true;
}

bool AnalyseTreeNodeConditionOR::DoExecute(const AnyData& input, std::wstring& wstrDescription) const
{
	if (m_listChildNode.size() < 2)
	{
		CommonFunc::ErrorMessageBox(CString("OR's operator is too few!"));
		return false;
	}
	
	if (wstrDescription.empty())
	{
		for (const auto& it : m_listChildNode)
		{
			CHECK_RE_FALSE(it.get());
			if (it->DoExecute(input, wstrDescription))
			{
				return true;
			}
		}
	}
	else
	{
		// 需要显示提示 就得把分支跑完
		bool bResult = false;
		for (const auto& it : m_listChildNode)
		{
			CHECK_RE_FALSE(it.get());
			if (it->DoExecute(input, wstrDescription))
			{
				bResult = true;
			}
		}
		return bResult;
	}
	
	return false;
}

bool AnalyseTreeNodeConditionOR::InsertNode(std::unique_ptr<AnalyseTreeNodeCondition> pNode)
{
	CHECK_RE_FALSE(pNode.get());
	m_listChildNode.push_back(std::move(pNode));
	return true;
}

bool AnalyseTreeNodeConditionAND::DoExecute(const AnyData& input, std::wstring& wstrDescription) const
{
	if (m_listChildNode.empty())
	{
		CommonFunc::ErrorMessageBox(CString("AND has no operator!"));
		return false;
	}
	
	if (wstrDescription.empty())
	{
		for (const auto& it : m_listChildNode)
		{
			CHECK_RE_FALSE(it.get());
			if (!it->DoExecute(input, wstrDescription))
			{
				return false;
			}
		}
	}
	else
	{
		// 需要显示提示 就得把分支跑完
		bool bResult = true;
		for (const auto& it : m_listChildNode)
		{
			CHECK_RE_FALSE(it.get());
			if (!it->DoExecute(input, wstrDescription))
			{
				bResult = false;
			}
		}
		return bResult;
	}
	return true;
}

bool AnalyseTreeNodeConditionAND::InsertNode(std::unique_ptr<AnalyseTreeNodeCondition> pNode)
{
	CHECK_RE_FALSE(pNode.get());
	m_listChildNode.push_back(std::move(pNode));
	return true;
}


